package com.rich.dmr.receiver;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.widget.Toast;

import com.rich.dmr.model.ChannelModel;
import com.rich.dmr.model.MessageEvent;
import com.rich.dmr.utils.Constant;
import com.rich.dmr.utils.DataBaseUtils;
import com.rich.dmr.utils.Log;
import org.greenrobot.eventbus.EventBus;
import java.util.List;
import java.util.Objects;
import rich.dmr.DmrManager;

public class ChannelSwitchReceiver extends BroadcastReceiver {
    String TAG = "rich---ChannelSwitchReceiver";
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.d(TAG, "intent.getAction()=" + intent.getAction());
        SharedPreferences mSp = context.getSharedPreferences(Constant.sp_key, Context.MODE_PRIVATE);
        long timeSelect = mSp.getLong("timeSelect", System.currentTimeMillis() - 2000);
        long temp = System.currentTimeMillis();
        if (temp - timeSelect > 300) {
            @SuppressLint("WrongConstant")
            DmrManager manager = (DmrManager) context.getSystemService("dmrservice");
            SharedPreferences.Editor mEditor = mSp.edit();
            List<ChannelModel> channelModels = DataBaseUtils.getInstance(context).findChannelList();
            int size = channelModels.size();
            if(size <= 0){
                //Toast.makeText(context,"no data",Toast.LENGTH_SHORT).show();
                return;
            }
            int index;
            boolean isMinus;

            switch (Objects.requireNonNull(intent.getAction())) {
                case "android.intent.action.KNOB_MINUS":
                    index = mSp.getInt("channelIndex", 0);
                    isMinus = mSp.getBoolean("channelMinus",false);
                    if(!isMinus){
                        if(index == (size-1)){
                            index = 1;
                        }else {
                            index = index + 2;
                        }
                    }
                    if (index < 0) {
                        index = 0;
                    } else if (index > size - 1) {
                        index = 0;
                    }
                    if (index <= size - 1) {
                        ChannelModel channelModel = channelModels.get(index);
                        manager.setTransmitFrequency(channelModel.getTxChannel());
                        manager.setReceiveFrequency(channelModel.getRxChannel());
                        manager.flush();
                        EventBus.getDefault().post(new MessageEvent(1,index));
                        //Toast.makeText(context, channelModel.getNameChannel(), Toast.LENGTH_SHORT).show();
                        //SoundPoolUtils.getInstance(context).playSound(false);
                        if (index < size - 1) {
                            index++;
                        } else if (index >= size - 1) {
                            index = 0;
                        }
                    }
                    Log.d(TAG, "index=" + index + " size=" + size);
                    mEditor.putBoolean("channelMinus",true);
                    mEditor.putInt("channelIndex", index);
                    mEditor.apply();
                    break;
                case "android.intent.action.KNOB_PLUS":
                    index = mSp.getInt("channelIndex", 0);
                    isMinus = mSp.getBoolean("channelMinus",false);
                    Log.d(TAG, "00000index=" + index);
                    if(isMinus){
                        if(index == 0){
                            index = size - 2;
                        }else {
                            index = index - 2;
                        }
                    }
                    if (index < 0) {
                        index = size - 1;
                    } else if (index > size - 1) {
                        index = size - 1;
                    }
                    if (index <= size - 1) {
                        ChannelModel channelModel = channelModels.get(index);
                        manager.setTransmitFrequency(channelModel.getTxChannel());
                        manager.setReceiveFrequency(channelModel.getRxChannel());
                        manager.flush();
                        EventBus.getDefault().post(new MessageEvent(1,index));

                        //Toast.makeText(context, channelModel.getNameChannel(), Toast.LENGTH_SHORT).show();
                        //SoundPoolUtils.getInstance(context).playSound(false);
                        if (index <= size - 1 && index > 0) {
                            index--;
                        } else if (index <= 0) {
                            index = size - 1;
                        }
                    }
                    Log.d(TAG, "index=" + index + " size=" + size);
                    mEditor.putBoolean("channelMinus",false);
                    mEditor.putInt("channelIndex", index);
                    mEditor.apply();
                    break;
                case "android.intent.action.KNOB_PRESS.down":
                    if (size > 0) {
                        ChannelModel channelModel = channelModels.get(0);
                        manager.setTransmitFrequency(channelModel.getTxChannel());
                        manager.setReceiveFrequency(channelModel.getRxChannel());
                        manager.flush();
                        EventBus.getDefault().post(new MessageEvent(1,0));
                        //Toast.makeText(context, channelModel.getNameChannel(), Toast.LENGTH_SHORT).show();
                        //SoundPoolUtils.getInstance(context).playSound(false);
                    }
                    mEditor.putInt("channelIndex", 0);
                    mEditor.apply();
                    break;
                default:
                    break;

            }
            timeSelect = System.currentTimeMillis();
            mEditor.putLong("timeSelect", timeSelect);
            mEditor.apply();
        }
    }
}
